'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 2_study_file_history.py
* @Time: 2025/7/20
* @All Rights Reserve By Brtc
'''
import dotenv
from langchain_community.chat_message_histories import FileChatMessageHistory
from openai import OpenAI

dotenv.load_dotenv()
chat_client = OpenAI(base_url="https://api.ephone.chat/v1")
chat_history = FileChatMessageHistory("./chat_history.txt", encoding="utf-8")
if __name__ == "__main__":
    while True:
        human_query = input("Human:")
        if human_query=="exit":
            print("Goodbye Human!!")
            exit(0)
        system_prompt = (
            "你是一个强大的聊天机器人，请根据对应的用户上下文和用户的问题来回答！上下文里面存储的是你跟人类对话的记录\n\n"
            f"<context>{chat_history}</context>"
        )
        completion = chat_client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role":"system","content":system_prompt},
                      {"role":"user",  "content":human_query}],
            stream=True
        )
        ai_content =""
        print("AI:", end="", flush=True)
        for chunk in completion:
            if len(chunk.choices) > 0:
                one_content = chunk.choices[0].delta.content
                if one_content is None:
                    break
                print(one_content, end="", flush=True)
                ai_content += one_content
        print("\n")
        chat_history.add_ai_message(ai_content)
        chat_history.add_user_message(human_query)


